package com.ymm.lulutong.domain.impl;

import com.ymm.lulutong.constant.ProfileCodeEnum;
import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperCargoDomainService;
import com.ymm.lulutong.domain.ShipperOrderDomainService;
import com.ymm.lulutong.domain.ShipperWalletDomainService;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.mapper.ShipperProfileMapper;
import com.ymm.lulutong.repo.SystemShipperOrderServiceRepo;
import com.ymm.lulutong.utils.Result;
import com.ymm.system.api.dto.ShipperCargoBriefResponse;
import com.ymm.system.api.dto.ShipperOrderBriefResponse;
import com.ymm.system.api.dto.ShipperOrderDetailResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author guorui
 */
@Slf4j
@Service
public class ShipperOrderDomainServiceImpl implements ShipperOrderDomainService {

    @Resource
    private SystemShipperOrderServiceRepo systemShipperOrderServiceRepo;

    @Resource
    private ShipperProfileMapper shipperProfileMapper;

    @Override
    public Result<Boolean> cancelOrderWithPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) throws Exception {
        if (orderId == null) {
            log.info("传入参数错误");
            return Result.fail(false, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return systemShipperOrderServiceRepo.cancelOrderWithPullOffCargo(orderId, shipperId, orderCancelReason, orderCancelDescription);
    }

    @Override
    public Result<Boolean> cancelOrderWithoutPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) throws Exception {
        if (orderId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return systemShipperOrderServiceRepo.cancelOrderWithoutPullOffCargo(orderId, shipperId, orderCancelReason, orderCancelDescription);
    }

    /**
     * 确认收货
     *
     * @param orderId 订单id
     * @return 结果
     */
    @Override
    public Result<Boolean> confirmReceipt(Long orderId, Long shipperId) throws Exception {
        if (orderId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return systemShipperOrderServiceRepo.confirmReceipt(orderId, shipperId);
    }

    @Override
    public Result<ShipperOrderDetailResponse> getOrderDetailByOrderId(Long orderId) {
        //调用系统接口查看订单详细信息
        if (orderId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return systemShipperOrderServiceRepo.getOrderDetailByOrderId(orderId);
    }

    @Override
    public Result<List<ShipperOrderBriefResponse>> listOngoingOrdersByShipperId(Long shipperId) {
        //调用系统接口查看进行中订单
        if (shipperId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        if (!shipperProfileMapper.existShipperId(shipperId)) {
            log.info("不存在的用户id");
            return Result.fail(null, ProfileCodeEnum.SHIPPER_NOT_EXIST.getCode(), ProfileCodeEnum.SHIPPER_NOT_EXIST.getMessage());
        }
        return systemShipperOrderServiceRepo.getOngoingOrderByShipperId(shipperId);
    }

    @Override
    public Result<List<ShipperOrderBriefResponse>> listCompletedOrdersByShipperId(Long shipperId) {
        //调用系统接口查看已完成订单
        if (shipperId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        if (!shipperProfileMapper.existShipperId(shipperId)) {
            log.info("不存在的用户id");
            return Result.fail(null, ProfileCodeEnum.SHIPPER_NOT_EXIST.getCode(), ProfileCodeEnum.SHIPPER_NOT_EXIST.getMessage());
        }
        Result<List<ShipperOrderBriefResponse>> completedOrdersResult = systemShipperOrderServiceRepo.getCompletedOrderByShipperId(shipperId);
        if (!completedOrdersResult.isSuccess() || completedOrdersResult.getData() == null) {
            return Result.fail(null, completedOrdersResult.getCode(), completedOrdersResult.getMessage());
        }
        List<ShipperOrderBriefResponse> completedOrders = completedOrdersResult.getData();
        completedOrders.sort((a, b) -> b.getOrderUpdateDate().compareTo(a.getOrderUpdateDate()));
        return Result.success(completedOrders);
    }

    @Override
    public Result<List<ShipperOrderBriefResponse>> listCancelledOrdersByShipperId(Long shipperId) {
        //调用系统接口查看已取消订单
        if (shipperId == null) {
            log.info("传入参数错误");
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        if (!shipperProfileMapper.existShipperId(shipperId)) {
            log.info("不存在的用户id");
            return Result.fail(null, ProfileCodeEnum.SHIPPER_NOT_EXIST.getCode(), ProfileCodeEnum.SHIPPER_NOT_EXIST.getMessage());
        }
        Result<List<ShipperOrderBriefResponse>> cancelledOrdersResult = systemShipperOrderServiceRepo.getCancelledOrderByShipperId(shipperId);
        if (!cancelledOrdersResult.isSuccess() || cancelledOrdersResult.getData() == null) {
            return Result.fail(null, cancelledOrdersResult.getCode(), cancelledOrdersResult.getMessage());
        }
        List<ShipperOrderBriefResponse> cancelledOrders = cancelledOrdersResult.getData();
        cancelledOrders.sort((a, b) -> b.getOrderUpdateDate().compareTo(a.getOrderUpdateDate()));
        return Result.success(cancelledOrders);
    }

    @Override
    public Result<String> getDriverPhoneNumber(Long orderId) {
        return systemShipperOrderServiceRepo.getDriverPhoneNumber(orderId);
    }
}
